package engine;

import java.awt.Color;
import java.awt.Point;

public class Edge
{
   private Color c;
   private Vertex firstNode, secondNode;
   private int recent = 0;
   private int status = 0; // this status if the edges is init active visited
   private int weight;

   public Edge(Vertex start, Vertex end, int weight)
   {
      this.firstNode = start;
      this.secondNode = end;
      this.weight = weight;
      status = 0;
      c = Color.GRAY;
   }

   public Edge(Vertex start, Vertex end, int weight, Color c)
   {
      this.firstNode = start;
      this.secondNode = end;
      this.weight = weight;
      this.c = c;
   }

   public Color getColor()
   {
      return c;
   }

   public String getColorToString()
   {
      if (c == Color.green)
         return "green";
      else if (c == Color.blue)
         return "blue";
      else if (c == Color.red)
         return "red";
      else if (c == Color.black)
         return "black";
      else if (c == Color.white)
         return "white";
      else
         return null;

   }

   public Point getEndPoint()
   {
      return secondNode.getPoint();
   }

   public Vertex getFirstNode()
   {
      return firstNode;
   }

   public Vertex getOther(Vertex v)
   {
      if (isFirstNode(v))
         return secondNode;
      else if (isSecondNode(v))
         return firstNode;
      else
         return null;
   }

   public int getRecentStatus()
   {
      return recent;
   }

   public Vertex getSecondNode()
   {
      return secondNode;
   }

   public Point getStartPoint()
   {
      System.out.println("First node " + firstNode.getPoint());
      return firstNode.getPoint();
   }

   public int getStatus()
   {
      return status;
   }

   public Vertex getUnvisitNode()
   {
      if (secondNode.isVisited() == false)
         return secondNode;
      else if (firstNode.isVisited() == false)
         return firstNode;
      else
         return null;
   }

   public int getWeight()
   {
      return weight;
   }

   public boolean isBothNodesVisit()
   {
      if ((firstNode.isVisited() && secondNode.isVisited()))
         return true;
      else
         return false;
   }

   public boolean isExist(Vertex v)
   {
      if (isFirstNode(v) || isSecondNode(v))
         return true;
      else
         return false;
   }

   public boolean isFirstNode(Vertex v)
   {
      return firstNode.equals(v);
   }

   public boolean isOtherVisited(Vertex v)
   {
      if (v == firstNode)
      {
         if (secondNode.getStatus() == 2)
            return true;
         else
            return false;
      }
      else if (v == secondNode)
         if (firstNode.getStatus() == 3)
            return true;
         else
            return false;
      return true;
   }

   public boolean isSecondNode(Vertex v)
   {
      return secondNode.equals(v);
   }

   public boolean isVisited()
   {
      if (status == 3)
         return true;
      return false;
   }

   public void setColor(Color c)
   {
      this.c = c;
   }

   public void setStatus(int s)
   {
      if (status != s)
         recent = status;
      status = s;

      if (status > 1)
         c = Color.BLACK;
      else
         c = Color.GRAY;
   }

   public void setWeight(int w)
   {
      this.weight = w;
   }

   public int xPos()
   {
      double z, x, y;
      x = secondNode.xPos() - firstNode.xPos();
      y = secondNode.yPos() - firstNode.yPos();
      z = Math.sqrt(x * x + y * y);
      return (firstNode.xPos() + (int) ((x / z) * (z / 2)));
   }

   public int yPos()
   {
      double z, x, y;
      x = secondNode.xPos() - firstNode.xPos();
      y = secondNode.yPos() - firstNode.yPos();
      z = Math.sqrt(x * x + y * y);
      return (firstNode.yPos() + (int) ((y / z) * (z / 2)));
   }
}
